#include "RingQueue.hpp"
#include "Task.hpp"
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include <sys/types.h>

void* productor(void* rq_)
{
    RingQueue<CalTask>* rq = static_cast<RingQueue<CalTask>*> (rq_);
    // RingQueue<int>* rq = static_cast<RingQueue<int>*> (rq_);
    while(true)
    {
        //version 1
        // int data = rand() % 10;
        // rq->push(data);
        // std::cout << "生产者生产数据: " << data << std::endl;
        // sleep(1);

        //version 2
        int x = rand() % 10;
        int y = rand() % 5;
        char op = math[rand() % math.size()];
        CalTask ct(x, y, mycal, op);
        rq->push(ct);
        std::cout << "生产者生产数据: " << ct.toTaskString() << std::endl;
    }
    return nullptr;
}

void* consumer(void* rq_)
{
    RingQueue<CalTask>* rq = static_cast<RingQueue<CalTask>*> (rq_);
    // RingQueue<int>* rq = static_cast<RingQueue<int>*> (rq_);
    while(true)
    {
        // //version 1
        // int data;
        // rq->pop(&data);
        // std::cout << "消费者消费数据: " << data << std::endl;
        // // sleep(1);

        //version 2
        CalTask ct;
        rq->pop(&ct);
        std::cout << "消费者消费数据: " << ct() << std::endl;
    }
    return nullptr;
}

int main()
{
    pid_t id = getpid();
    srand((unsigned int)time(nullptr) ^ 0x123135 ^ id);
    RingQueue<CalTask>* rq = new RingQueue<CalTask> ();
    // RingQueue<int>* rq = new RingQueue<int> ();
    pthread_t p, c;
    pthread_create(&p, nullptr, productor, (void*)rq);
    pthread_create(&c, nullptr, consumer, (void*)rq);
    
    pthread_join(p, nullptr);
    pthread_join(c, nullptr);

    return 0;
}